Public Class CreateOrderCompleteInfo
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
    Protected WithEvents txtDepartureDate As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtFreightForwarder As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtContainerNumber As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtComment As System.Web.UI.WebControls.TextBox
    Protected WithEvents hidFreightForwarder As System.Web.UI.HtmlControls.HtmlInputHidden
    Protected WithEvents lblErr As System.Web.UI.WebControls.Label
    Protected WithEvents txtDestination As System.Web.UI.WebControls.TextBox
    Protected WithEvents lblTitle As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        If (Not Page.IsPostBack) And (PageBO.CompleteInfo) Then

            'Display all complete information of the order.
            lblTitle.Text = "Edit Order's Complete Information"
            DisplayCompleteInformation()

        End If
    End Sub

    Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Try
            UpdateCompleteInformation()

            Me.SetReturnValue()
        Catch ex As Exception
            Me.lblErr.Text = ex.Message
        End Try
    End Sub

    Protected ReadOnly Property PageBO() As COrder
        Get
            Return CType(Session(OrderInput.ORDER_INPUT_PAGEBO_SESSION_NAME), COrder)
        End Get
    End Property

    Private Sub DisplayCompleteInformation()
        txtDepartureDate.Text = MIMS_WRS.Business.Common.FormatDate(PageBO.DepartureDate)
        txtDestination.Text = PageBO.Destination
        txtFreightForwarder.Text = GetFreightForwarder(PageBO.FreightID)
        hidFreightForwarder.Value = PageBO.FreightID.ToString
        txtContainerNumber.Text = PageBO.ContainerNumber
        txtComment.Text = PageBO.Comments

    End Sub

    Private Function GetFreightForwarder(ByVal freightID As Integer) As String
        Try
            Dim obj As New CLookupData
            obj.Load(freightID)

            Return obj.Value1
        Catch ex As Exception
            Return ""
        End Try
    End Function

    Private Sub UpdateCompleteInformation()
        Try
            Dim result As Integer
            Dim departureDate As System.DateTime
            Dim freightID As Integer

            If (txtDepartureDate.Text.Trim().Length > 0) Then
                departureDate = CDate(txtDepartureDate.Text)
            Else
                departureDate = CDate("1/1/1753")
            End If

            If (hidFreightForwarder.Value.Trim().Length > 0) Then
                freightID = CInt(hidFreightForwarder.Value)
            End If

            result = COrder.UpdateCompleteInformation(PageBO.ID, departureDate, txtDestination.Text.Trim(), freightID, txtContainerNumber.Text.Trim(), txtComment.Text.Trim())
            If result < 0 Then
                Throw New ApplicationException("Order Complete Information has been changed unsuccessfully!")
            End If
        Catch ex As Exception
            Throw New ApplicationException(ex.Message)
        End Try
    End Sub

    Private Sub SetReturnValue()
        Dim s As String = ""
        s += "var obj= new Object();"
        s += String.Format("obj.HaveCompleteInfo = '{0}';", "True")
        s += "window.returnValue = obj;"
        s += "window.close();"
        Response.Write(String.Format("<script>{0}</script", s))
    End Sub
End Class
